<template>
  <div class="app-container">
    <el-form :model="queryParams.domain" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px" @submit.native.prevent>
      <el-form-item label="密钥" prop="secret">
        <el-input
          v-model="queryParams.domain.secret"
          placeholder="请输入密钥"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:secret:add','profile:secret:add']"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="dataList" >
      <el-table-column label="密钥" align="center" prop="secret" />
      <el-table-column label="用户key" align="center" prop="userKey" />
      <el-table-column label="用户pwd" align="center" prop="userPwd"  />
      <el-table-column label="创建时间" align="center" prop="createDate" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:secret:remove','profile:secret:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改岗位对话框 -->
    <el-dialog title="新增密钥" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form"  label-width="80px">
        <el-form-item label="密钥" prop="secret">
          <el-input disabled="true" v-model="form.secret" placeholder="密钥" />
        </el-form-item>
        <el-form-item label="用户key" prop="userKey">
          <el-input  disabled="true"  v-model="form.userKey" placeholder="用户key" />
        </el-form-item>
        <el-form-item label="用户pwd" prop="userPwd">
          <el-input disabled="true" v-model="form.userPwd" placeholder="用户pwd" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSecret, addSecret, delSecret,genSecret } from "@/api/system/secret.js";

export default {
  name: "Post",
  dicts: ['sys_normal_disable'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 岗位表格数据
      dataList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        domain: {
            title: ""
        },
        pageNum: 1,
        pageSize: 10,
        sortName: "",
        sortOrder: ""
      },
      // 表单参数
      form: {},
    };
  },
  created() {

    this.getList();
  },
  methods: {

    /** 查询岗位列表 */
    getList() {
      this.loading = true;
      listSecret(this.queryParams).then(response => {
        this.dataList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = { }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      genSecret().then(res =>{
         this.reset();
        this.form = res.data;
         this.open = true;
      })
    },
    /** 提交按钮 */
    submitForm: function() {
     addSecret(this.form).then(response => {
       this.$modal.msgSuccess("添加成功");
       this.open = false;
       this.getList();
     });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id;
      this.$confirm('是否确认删除此条通知？').then(function() {
        return removeInfo(id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
  }
};
</script>
